/*
Run this script on:

        mssql-tl-test.db.jwan.it,14444.TTTLINES2014_test    -  This database will be modified

to synchronize it with:

        kalgan01.cs.jwan.it,1443.Jambo2014_sviluppo

You are recommended to back up your database before running this script

Script created by SQL Compare version 9.0.0 from Red Gate Software Ltd at 02/04/2014 12:34:40

*/
SET NUMERIC_ROUNDABORT OFF
GO
SET ANSI_PADDING, ANSI_WARNINGS, CONCAT_NULL_YIELDS_NULL, ARITHABORT, QUOTED_IDENTIFIER, ANSI_NULLS ON
GO
IF EXISTS (SELECT * FROM tempdb..sysobjects WHERE id=OBJECT_ID('tempdb..#tmpErrors')) DROP TABLE #tmpErrors
GO
CREATE TABLE #tmpErrors (Error int)
GO
SET XACT_ABORT ON
GO
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
GO
BEGIN TRANSACTION
GO
PRINT N'Creating [dbo].[v_jb_abbonamenti_passaggi_giornalieri]'
GO
CREATE VIEW dbo.v_jb_abbonamenti_passaggi_giornalieri
WITH ENCRYPTION AS
SELECT     m.id_tessera,
                          (SELECT     dbo.fn_jb_compagnia()) AS CODCOMP, /*m.NUMBIGL,*/ m.ORASECONDI AS DATAMOV, m.TIPOMOV
FROM         v_jb_MovCassaDettBigl m
WHERE     m.IS_PASSAGGIO_TESSERA = 1 AND orasecondi >= dateadd(day, datediff(day, 0, GETDATE()), 0) AND orasecondi < dateadd(day, 1, dateadd(day, datediff(day, 0, 
                      GETDATE()), 0))
UNION
SELECT     ID_TESSERA, CODCOMP, DATAMOV, TIPOMOV
FROM         jb_remote_abbonamenti_passaggi
WHERE     DATAMOV >= dateadd(day, datediff(day, 0, GETDATE()), 0) AND DATAMOV < dateadd(day, 1, dateadd(day, datediff(day, 0, GETDATE()), 0))
GO
IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION
GO
IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors (Error) SELECT 1 BEGIN TRANSACTION END
GO
PRINT N'Creating [dbo].[v_jb_abbonamenti_validi]'
GO



-- =============================================
-- AM 2012-09-18 - Gestione clienti locali e remoti
-- VM 2012-08-07 - Aggiornato alla nuova struttura
-- Author:		Andrea Marchesini
-- Create date: 2010-11-11
-- Description:	Fornisce gli abbonamenti validi
-- =============================================
CREATE VIEW [dbo].[v_jb_abbonamenti_validi]

--WITH ENCRYPTION
 WITH ENCRYPTION AS

 select 
	t.ID_TESSERA 
	,t.CODCOMP
	,t.IDCLIENTE
	,t.CODCOMP as CODCOMP_TESSERA
	,a.CODCOMP as CODCOMP_RINNOVO
	,t.TIPO_TESSERA
	,t.COD_ITI_PORT_A
	,t.COD_ITI_PORT_r
	,T.ISPORT
	,t.FESTIVI
	,t.NUMPASS_GIORNALIERI
	,a.IDRINNOVO
	,a.DATA_INIZIO
	,a.DATA_FINE
	,t.WITH_FOTO

 from jb_abbonamenti a
 join v_jb_abbonamenti_clienti t on a.ID_TESSERA = t.ID_TESSERA --a.numtessera = t.numtessera 			
			and t.annullato = 0
			and a.ANNULLATO = 0
			and  getdate() between a.data_inizio and a.data_fine






GO
IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION
GO
IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors (Error) SELECT 1 BEGIN TRANSACTION END
GO
PRINT N'Creating extended properties'
GO
EXEC sp_addextendedproperty N'MS_DiagramPane1', N'[0E232FF0-B466-11cf-A24F-00AA00A3EFFF, 1.00]
Begin DesignProperties = 
   Begin PaneConfigurations = 
      Begin PaneConfiguration = 0
         NumPanes = 4
         Configuration = "(H (1[40] 4[20] 2[20] 3) )"
      End
      Begin PaneConfiguration = 1
         NumPanes = 3
         Configuration = "(H (1 [50] 4 [25] 3))"
      End
      Begin PaneConfiguration = 2
         NumPanes = 3
         Configuration = "(H (1 [50] 2 [25] 3))"
      End
      Begin PaneConfiguration = 3
         NumPanes = 3
         Configuration = "(H (4 [30] 2 [40] 3))"
      End
      Begin PaneConfiguration = 4
         NumPanes = 2
         Configuration = "(H (1 [56] 3))"
      End
      Begin PaneConfiguration = 5
         NumPanes = 2
         Configuration = "(H (2 [66] 3))"
      End
      Begin PaneConfiguration = 6
         NumPanes = 2
         Configuration = "(H (4 [50] 3))"
      End
      Begin PaneConfiguration = 7
         NumPanes = 1
         Configuration = "(V (3))"
      End
      Begin PaneConfiguration = 8
         NumPanes = 3
         Configuration = "(H (1[56] 4[18] 2) )"
      End
      Begin PaneConfiguration = 9
         NumPanes = 2
         Configuration = "(H (1 [75] 4))"
      End
      Begin PaneConfiguration = 10
         NumPanes = 2
         Configuration = "(H (1[66] 2) )"
      End
      Begin PaneConfiguration = 11
         NumPanes = 2
         Configuration = "(H (4 [60] 2))"
      End
      Begin PaneConfiguration = 12
         NumPanes = 1
         Configuration = "(H (1) )"
      End
      Begin PaneConfiguration = 13
         NumPanes = 1
         Configuration = "(V (4))"
      End
      Begin PaneConfiguration = 14
         NumPanes = 1
         Configuration = "(V (2))"
      End
      ActivePaneConfig = 0
   End
   Begin DiagramPane = 
      Begin Origin = 
         Top = 0
         Left = 0
      End
      Begin Tables = 
      End
   End
   Begin SQLPane = 
   End
   Begin DataPane = 
      Begin ParameterDefaults = ""
      End
   End
   Begin CriteriaPane = 
      Begin ColumnWidths = 11
         Column = 1440
         Alias = 900
         Table = 1170
         Output = 720
         Append = 1400
         NewValue = 1170
         SortType = 1350
         SortOrder = 1410
         GroupBy = 1350
         Filter = 1350
         Or = 1350
         Or = 1350
         Or = 1350
      End
   End
End
', 'SCHEMA', N'dbo', 'VIEW', N'v_jb_abbonamenti_passaggi_giornalieri', NULL, NULL
GO
IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION
GO
IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors (Error) SELECT 1 BEGIN TRANSACTION END
GO
EXEC sp_addextendedproperty N'MS_DiagramPaneCount', 1, 'SCHEMA', N'dbo', 'VIEW', N'v_jb_abbonamenti_passaggi_giornalieri', NULL, NULL
GO
IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION
GO
IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors (Error) SELECT 1 BEGIN TRANSACTION END
GO
PRINT N'Altering permissions on [dbo].[v_jb_abbonamenti_passaggi_giornalieri]'
GO
GRANT SELECT ON  [dbo].[v_jb_abbonamenti_passaggi_giornalieri] TO [JamboClient]
GO
PRINT N'Altering permissions on [dbo].[v_jb_abbonamenti_validi]'
GO
GRANT SELECT ON  [dbo].[v_jb_abbonamenti_validi] TO [JamboClient]
GRANT SELECT ON  [dbo].[v_jb_abbonamenti_validi] TO [JamboGest]
GO
IF EXISTS (SELECT * FROM #tmpErrors) ROLLBACK TRANSACTION
GO
IF @@TRANCOUNT>0 BEGIN
PRINT 'The database update succeeded'
COMMIT TRANSACTION
END
ELSE PRINT 'The database update failed'
GO
DROP TABLE #tmpErrors
GO
